package UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;



import utils.FichierTexte;
import t2s.SIVOXDevint;


import MainLaunch.Main;
import MainLaunch.StaticFields;

public class LoadGame extends JFrame implements ActionListener, ListSelectionListener {

	
	private JButton charger;
	private JButton go;
	private File file;
	private JLabel nom;
	private Vector values;
	private JPanel panelList;
	private JScrollPane scrollpan;
	private JList list;
	private JPanel panelGo;
	private JPanel panelname;
	private FichierTexte fichier; 
	private Window window;
	public SIVOXDevint sv; 
	
	public LoadGame(Window window, FichierTexte fichier) {
		this.fichier=fichier;
		this.window=window;
		this.setTitle("load a game");
		values = new Vector();
		addReadValues();
		list=new JList(values);
		if (values.firstElement()!=null){
			list.setSelectedValue(values.firstElement(), true);
			file = new File(values.firstElement().toString());
		}
		charger = new JButton("load from file");
		go = new JButton("load");
		nom = new JLabel();
		sv = new SIVOXDevint();
		
		panelList = new JPanel();
		scrollpan=new JScrollPane();
		panelGo = new JPanel();
		panelname = new JPanel();
		
		list.setToolTipText("Game recently used");
		list.setPrototypeCellValue("C://Document and settings/blablabla/blablabla/blablabla/blablabla/blablablablabla/blablalblalblba");
		panelList.setLayout(new BorderLayout());
		panelList.add(list,BorderLayout.CENTER);
		panelList.add(scrollpan,BorderLayout.EAST);
	
		panelname.setBackground(Color.LIGHT_GRAY);
		panelname.add(nom);
		
		scrollpan.getViewport().setView(list);
		BorderLayout bd = new BorderLayout();
		panelGo.setLayout(bd);
		panelGo.add(charger,bd.NORTH);
		panelGo.add(go,bd.CENTER);
		
		this.setLayout(new BorderLayout());
		this.add(panelList,BorderLayout.WEST);
		this.add(panelGo,BorderLayout.EAST);
		this.add(panelname,BorderLayout.SOUTH);
	
		this.setSize(710, 220);
		Toolkit t = this.getToolkit();
		this.setLocation((int)(t.getScreenSize().getWidth()-this.getSize().getWidth())/2, (int)(t.getScreenSize().getHeight()-this.getSize().getHeight())/2);
		this.setVisible(true);
		
		charger.addActionListener(this);
		go.addActionListener(this);
		list.addListSelectionListener(this);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		// on lance si il y a que un element dans la liste
		if (this.values.size()==1){
			System.out.println("go");
			go.doClick();
		}
	}
	
	

	private void addReadValues() {
		LinkedList<String> list = fichier.lire();
		while (!list.isEmpty()){
			values.add(new File(list.removeLast()));
			
		}
	}

	private void saveValues(){
		fichier.InitialiseEcrireDansFichierTexte();
		for (int i=0;i<values.size();i++){
			fichier.EcrireDansFichierTexte(values.get(i).toString());
		}
		fichier.FermerEcrireDansFichierTexte();
	}


	



	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource()==charger){
			file = this.openDialogFindFile();
			if (file!=null){
				values.add(file);
				list.setListData(values);
				sv.playText(file.getName().toString());
				nom.setText(file.toString());
			}
		}
		if (arg0.getSource()==go){
			if (file!=null){
				saveValues();
				window.LoadAClassWithHisNameFile(file);
				this.dispose();
				
			}
			else Main.Erreur("nothing to load");
		}
	}

	
	/** open a dialog in order to open classfile
	 * @return
	 */
	private File openDialogFindFile(){
		JFileChooser jf = new JFileChooser ();
		int returnVal = jf.showOpenDialog(this);
		if(returnVal == JFileChooser.APPROVE_OPTION) {
			return jf.getSelectedFile();
		}
		return null;
	}



	public void valueChanged(ListSelectionEvent arg0) {
		if (arg0.getSource()==list){
			File temp = (File) list.getSelectedValue();
			if (temp!=null)file=(File) list.getSelectedValue();
			if (file!=null){
				sv.playText(file.getName().toString());
				nom.setText(file.toString());
			}
		}
		
	}





	
}
